package com.hl.secondHand.controller;


import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hl.secondHand.common.lang.Result;
import com.hl.secondHand.common.vo.EasyGoods;
import com.hl.secondHand.entity.Goods;
import com.hl.secondHand.entity.User;
import com.hl.secondHand.mapper.GoodsMapper;
import com.hl.secondHand.service.IGoodsService;
import com.hl.secondHand.service.IUserService;
import com.hl.secondHand.shiro.AccountProFile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hl
 * @since 2021-06-06
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    IUserService userService;
    @Autowired
    IGoodsService goodsService;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    MailboxController mailboxController;
    @Autowired
    OrdersController ordersController;

    @RequestMapping("/getEasyGoodsList")
    public Result getEasyGoodsList(){
        List<EasyGoods> goods = goodsMapper.getEasyGoods();
        for(EasyGoods tgoods : goods){
            tgoods.setImage(tgoods.getImage()==null?"":tgoods.getImage().split("/;")[0]);
        }
        return Result.succes(goods);
    }

    @RequiresAuthentication
    @RequestMapping("/getGoodsList")
    public Result getGoodsList(){
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        List<Goods> goods = goodsService.list(new QueryWrapper<Goods>().eq("uid",nowId).orderByDesc("last_modified_date"));
        for(Goods tgoods : goods){
            tgoods.setImage(tgoods.getImage()==null?"":tgoods.getImage().split("/;")[0]);
        }
        return Result.succes(goods);
    }

    @RequestMapping("/getGoods")
    public Result getGoods(int id){
        Goods goods = goodsService.getOne(new QueryWrapper<Goods>()
                .eq("id", id)
                .orderByAsc("id"));
        Assert.isTrue(goods.getState()==1,"本书已被购买或删除!");
        return Result.succes(goods);
    }

    @RequiresAuthentication
    @RequestMapping("/edit")
    public Result edit(Goods goods) {
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        Goods temp ;
        if (goods.getId() != null) { //如果传了id，那就代表是编辑
            temp = goodsService.getById(goods.getId());
            Assert.isTrue(goods.getUid() == nowId, "无权编辑");
        } else {  //新增
            temp = new Goods();
            temp.setUid(nowId);
            temp.setState(1);
            temp.setWant(0);
        }
        temp.setLastModifiedDate(LocalDateTime.now());
        BeanUtils.copyProperties(goods, temp, "id", "uid", "createdDate", "state","purchase");
        goodsService.saveOrUpdate(temp);
        return Result.succes(null);
    }

    @RequiresAuthentication
    @RequestMapping("/editState")
    public Result editState(int id,int state) {
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        Goods goods = goodsService.getById(id);
        goods.setState(state);
        goodsService.saveOrUpdate(goods);
        return Result.succes(null);
    }

    @RequiresAuthentication
    @RequestMapping("/purchase")
    public Result purchase(int id,String remarks) {
        int nowId = ((AccountProFile) SecurityUtils.getSubject().getPrincipal()).getId();
        User user = userService.getOne(new QueryWrapper<User>().eq("id", nowId));
        Goods goods = goodsService.getOne(new QueryWrapper<Goods>().eq("id", id).eq("state",1));
        Assert.notNull(goods,"本商品已被购买或删除!");
        float nowBalance = user.getBalance() - goods.getAmount();
        Assert.isTrue(nowBalance >=0, "余额不足，请充值！");
        user.setBalance(nowBalance);
        userService.saveOrUpdate(user);
        goods.setState(2);
        goodsService.saveOrUpdate(goods);
        ordersController.addOrder(id,remarks);
        mailboxController.addMail(goods.getUid(),2,"您的书籍《"+goods.getName()+"》已被购买");
        return Result.succes(null);
    }

    @RequestMapping("/addWant")
    public Result addWant(int id) {
        Goods goods = goodsService.getById(id);
        goods.setWant(goods.getWant()+1);
        goodsService.saveOrUpdate(goods);
        return Result.succes(goods);
    }

    @RequestMapping("/getCarousel")
    public Result getCarousel() {
        List<Goods> goods = goodsService.list(new QueryWrapper<Goods>().select("id","image").eq("state",1).orderByDesc("want").orderByDesc("id").last("limit 0,6"));
        for(Goods tgoods : goods){
            tgoods.setImage(tgoods.getImage()==null?"":tgoods.getImage().split("/;")[0]);
        }
        return Result.succes(goods);
    }
}
